import brandApi from "@/api/brandApi";

export default {

   created() {
       this.searchPage();
   },
    name: "Index.vue",
    data(){
        return{
            //弹窗
            dialogVisible:false,

            //时间
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            value1: '',
            value2: '',


            //搜索返回的数据
            searchForm:{
                currentPage:1,
                size:5,


            },
            ids:[],

            //列表
            tableData: [],

            total:0,

            formDate:{
                brandLogo:"",

            },


        }



    },

    methods:{
        // 逻辑方法
        /*搜索*/
      async searchPage(){
          let resoult =  await brandApi.search(this.searchForm)
          this.tableData = resoult.data
          this.total=resoult.total

      },

        /*添加*/
        async addOrUpdate(){

           if (this.formDate.id){
               await brandApi.update(this.formDate)
           }else {
               await brandApi.inster(this.formDate)
           }
            this.searchPage()
        },


        /*删除*/
       async delBrand(){
           await brandApi.deleByIds(this.ids)
           this.searchPage()
        },



        /*批量删除*/
        async delsBrand() {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.delBrand()

            }).catch(() => {
                this.$refs.table.clearSelection();
                this.ids=[]

            });


    },


        /*根据id查询*/
        async  searchById(id){
            this.formDate = await brandApi.searchById(id)
        },


        //页面方法

        //时间改变获取值
        timeChange(value){
            this.searchForm.startTime=value[0]
            this.searchForm.endTime=value[1]
            console.log(value)
        },

        //搜索
        selectBrand(){
            this.searchPage()
        },


        /*分页*/
        changePage(page){
            this.searchForm.currentPage=page
            this.searchPage()
        },

        /*重置*/
        reseBrand(){
            this.searchForm.brandName=''
            this.value2=''
            this.searchPage()
        },

        /*上传图片*/
        brandImgBase(e){
            console.log(e)
            var fileReader = new FileReader()
            fileReader.readAsDataURL(e.file)
            fileReader.onloadend = (e)=>{
                this.formDate.brandLogo=e.target.result
            }
        },

        /*获得id*/
        getId(id){
            this.ids=[]
            this.ids = id

        },


        /*批量获得id*/
        selectChang(value){
            this.ids=[]
            var map = value.map(item => item.id)
            map.forEach(item =>  this.ids.push(item))

        }

    }
}